package com.neusoft.neumooc.controller.front;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.util.Util;
import com.neusoft.neumooc.service.front.ContentService;
import com.neusoft.neumooc.vo.ColumnsVO;
import com.neusoft.neumooc.vo.ContentVO;
import com.neusoft.neumooc.vo.MenuVO;

@Controller
@RequestMapping(value = "/content")
public class ContentController extends BaseController {
	@Autowired
	ContentService contentservice;

	@RequestMapping(value = "/columns")
	@ResponseBody
	public Map<String, Object> columns(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String columnId = request.getParameter("link");
		MenuVO menuVo = contentservice.getMenuNameByColumnId(columnId);
		String menuNameUs = contentservice.getMenuNameUsByColumnId(columnId);
		String columnName = contentservice.getColumnNameByColumnId(columnId);
		String typeId = contentservice.getTypeIdByColumnId(columnId);
		List<ColumnsVO> columnsVo = contentservice.getColumnsVo(typeId);
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("columnsVo", columnsVo);
		resMap.put("columnName", columnName);
		resMap.put("menuVo", menuVo);
		resMap.put("menuNameUs", menuNameUs);
		return resMap;
	}

	@RequestMapping(value = "/contents")
	@ResponseBody
	public Map<String, Object> contents(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String columnId = request.getParameter("link");
		MenuVO menuVo = contentservice.getMenuNameByColumnId(columnId);
		String columnName = contentservice.getColumnNameByColumnId(columnId);
		String menuNameUs = contentservice.getMenuNameUsByColumnId(columnId);
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		pageNo = pageNo == null ? "1" : pageNo;
		pageSize = pageSize == null ? "3" : pageSize;
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);
		int contentNum = contentservice.getContentVoNum(columnId);
		List<ContentVO> contentVo = contentservice.getContentVo(columnId,
				pageIndex, Util.parseInt(pageSize));
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("contentVo", contentVo);
		resMap.put("contentNum", contentNum);
		resMap.put("columnName", columnName);
		resMap.put("menuVo", menuVo);
		resMap.put("menuNameUs", menuNameUs);
		return resMap;
	}

	@RequestMapping(value = "/leader")
	public String leader(Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String columnId = request.getParameter("link");
		String typeId = contentservice.getTypeIdByColumnId(columnId);
		String listTplFile = contentservice.getListTplFileByColumnId(columnId);
		String pageTplFile = contentservice.getPageTplFileByColumnId(columnId);
		model.addAttribute("columnId", columnId);
		model.addAttribute("typeId", typeId);
		model.addAttribute("listTplFile", listTplFile);
		model.addAttribute("pageTplFile", pageTplFile);
		return "front/commonPage";
	}

	/**
	 * @Title: detail
	 * @Description: 获得详情数据
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Map<String, Object> detail(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String contentId = request.getParameter("contentId");
		List<ContentVO> contentDetail = contentservice
				.getContentDetail(contentId);
		String title = contentservice.getTitleByContentId(contentId);
		String columnId = contentservice.getColumnIdByContentId(contentId);
		MenuVO menuVo = contentservice.getMenuNameByColumnId(columnId);
		String columnName = contentservice.getColumnNameByColumnId(columnId);
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("contentVo", contentDetail);
		resMap.put("title", title);
		resMap.put("columnName", columnName);
		resMap.put("menuVo", menuVo);
		return resMap;
	}

	/**
	 * @Title: getSearcInfo
	 * @Description: 获取搜索数据
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSearcInfo")
	@ResponseBody
	public Map<String, Object> getSearcInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String search = request.getParameter("search");
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		pageNo = pageNo == null ? "1" : pageNo;
		pageSize = pageSize == null ? "5" : pageSize;
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);
		int searchNum = contentservice.getSearchInfoNum(search);
		List<ContentVO> searchVo = contentservice.getSearchInfo(search,
				pageIndex, Util.parseInt(pageSize));
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("searchVo", searchVo);
		resMap.put("searchNum", searchNum);
		return resMap;
	}
}
